#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.5
#####

library(here)
library(data.table)
library(texreg)

# source the custom functions
source("functions.R")

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}

# define the imbalanced covaraites
imbRep <- c("race2", "edu2", "pid2", "region2")


# Fitting -----------------------------------------------------------------

s5_1 <- noCov(mechanism_NM, sample = "Replication", chatty = F)
s5_2 <- allCov(mechanism_NM, sample = "Replication", chatty = F)
s5_3 <- flexCov(mechanism_NM, sample = "Replication", covs = imbRep, chatty = F)

s5_4 <- noCov(mechanism_CS, sample = "Replication", chatty = F)
s5_5 <- allCov(mechanism_CS, sample = "Replication", chatty = F)
s5_6 <- flexCov(mechanism_CS, sample = "Replication", covs = imbRep, chatty = F)

s5_7 <- noCov(mechanism_CH, sample = "Replication", chatty = F)
s5_8 <- allCov(mechanism_CH, sample = "Replication", chatty = F)
s5_9 <- flexCov(mechanism_CH, sample = "Replication", covs = imbRep, chatty = F)


# Reporting ---------------------------------------------------------------

# in the document, these three outputs are merged to a single latex table.

texregPS(
  list(s5_1, s5_2, s5_3),
  custom.model.names = paste0("(", 1:3, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s5_4, s5_5, s5_6),
  custom.model.names = paste0("(", 4:6, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s5_7, s5_8, s5_9),
  custom.model.names = paste0("(", 7:9, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)
